package com.example.androidgeneric;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;

public class F_ImageGallery extends Fragment {

	ImageView img;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		img = new ImageView(getActivity());

		return img;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		ImageListener imgListener = ImageLoader.getImageListener(img,
				R.drawable.ic_launcher, R.drawable.ic_launcher);

		img.setImageResource(android.R.color.holo_blue_bright);
		
		MyApplication
				.getInstance()
				.getImageLoader()
				.get("http://cfile29.uf.tistory.com/image/163EE636503470B415B640",
						new mImageListener(img));

	}

	private class mImageListener implements ImageListener {

		private static final int fadeInDuration = 500;
		private static final int timeBetween = 3000;
		private static final int fadeOutDuration = 1000;

		private ImageView img;
		private AnimationSet ani;

		public mImageListener(ImageView img) {
			this.img = img;
			setAnimation();
		}

		private void setAnimation() {
			Animation fadeIn = new AlphaAnimation(0, 1);
			fadeIn.setInterpolator(new DecelerateInterpolator());
			fadeIn.setDuration(fadeInDuration);

			// Animation fadeOut = new AlphaAnimation(1, 0);
			// fadeOut.setInterpolator(new AccelerateInterpolator());
			// fadeOut.setStartOffset(fadeInDuration + timeBetween);
			// fadeOut.setDuration(fadeOutDuration);

			ani = new AnimationSet(false);
			ani.addAnimation(fadeIn);
			// ani.addAnimation(fadeOut);
			ani.setRepeatCount(1);
		}

		@Override
		public void onErrorResponse(VolleyError arg0) {
			img.setImageResource(R.drawable.ic_launcher);
		}

		@Override
		public void onResponse(ImageContainer response, boolean arg1) {
			if (response.getBitmap() != null) {
				img.setImageBitmap(response.getBitmap());
				img.setAnimation(ani);
			} else {
				img.setImageResource(getResources().getColor(android.R.color.holo_orange_dark));
			}
		}

	}

	// private void setListview() {
	//
	// }
	//
	// private class mAdapter extends ArrayAdapter<String> {
	//
	// public mAdapter(Context context, int resource, List<String> objects) {
	// super(context, resource, objects);
	// // TODO Auto-generated constructor stub
	// }
	//
	// @Override
	// public View getView(int position, View convertView, ViewGroup parent) {
	//
	// if (convertView == null) {
	// convertView = new ImageView(getActivity());
	// }
	//
	//
	// ImageLoader.getImageListener((ImageView) convertView,
	// R.drawable.ic_launcher, R.drawable.ic_launcher);
	//
	// ImageRequest request = new ImageRequest(url, listener, maxWidth,
	// maxHeight, decodeConfig, errorListener)
	//
	// return super.getView(position, convertView, parent);
	// }
	// }
}
